package com.liuxingzhiwu.ruijishangcheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuxingzhiwu.ruijishangcheng.common.R;
import com.liuxingzhiwu.ruijishangcheng.entity.AddressBook;
import com.liuxingzhiwu.ruijishangcheng.service.AddressBookService;
import lombok.Lombok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 显示收件地址
     * @return
     */
    @GetMapping("/list")
    public R<List> getAddressBook(){

        List<AddressBook> list = addressBookService.list();

        return R.success(list);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @Transactional
    public R<String> setDefaultAddressBook(@RequestBody AddressBook addressBook){
        //将原来的默认地址修改非默认
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_default",1);
        AddressBook addressBook1 = new AddressBook();
        addressBook1.setIsDefault(0);
        addressBook1.setUpdateTime(LocalDateTime.now());
        addressBook1.setUpdateUser(001L);
        addressBookService.update(addressBook1,queryWrapper);

        //设置默认地址
        addressBook.setIsDefault(1);
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setUpdateUser(001L);
        QueryWrapper<AddressBook> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",addressBook.getId());
        addressBookService.update(addressBook,queryWrapper1);
        return R.success("修改成功");
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){

        addressBook.setUserId(001L);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateUser(001L);
        addressBook.setUpdateUser(001L);
        addressBook.setIsDefault(0);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 回显地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBook(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);

        return R.success(addressBook);
    }

    /**
     * 更新地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddressBook(Long ids){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",ids);
        addressBookService.remove(queryWrapper);
        return R.success("删除成功");
    }
}
